package com.javaweb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

@WebFilter(filterName = "FilterEncoding",
       /* urlPatterns = {"/*"},*/
        initParams = {@WebInitParam(name="encoding",value="UTF-8")}
)
public class FilterEncoding implements Filter {
    private String encoding = "" ;
 //   private static final String Encoding = "UTF-8" ;
     @Override
    public void destroy() {
         System.out.println("the encoding filter was destroyed.") ;

    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /**
         * 当请求到达时， 过滤逻辑可以定义在这
         */
        req.setCharacterEncoding(this.encoding);
       // System.out.println("[INFO] before processed the request.") ;
         chain.doFilter(req, resp);
        /**
         * 请求处理后的逻辑可以定义在这
         */
        //System.out.println("[INFO] after processed the request") ;
    }
    @Override
    public void init(FilterConfig config) throws ServletException {
        this.encoding = config.getInitParameter("encoding") ;
        System.out.println("the encoding filter was initialized.") ;
    }

}
